<?php
define('KC_INDEX',True);

require_once '../global.php';


//数据处理





function king_def(){
	global $king;

	$kid=kc_get('kid','0');
	$typeid =kc_get('typeid','0');
	$data['k_typeid'] =kc_get('typeid','0');
	
	
	
	$king->load('user');//加载用户判断	
	$king->load('portal');//
	$user=$king->user->checkLogin();//判断用户是否登录
	
	$but='<a href="javascript:;" class="k_ajax" rel="{URL:\''.$king->config('inst').'user/index.php\',CMD:\'login\',METHOD:\'GET\',kuidb:\''.$kid.'\',typeid:\''.$typeid.'\'}">登陆并发布信息</a> ';
	
	$register='<a href="javascript:;" class="k_ajax" rel="{URL:\''.$king->config('inst').'user/index.php\',CMD:\'register\',METHOD:\'GET\',kuidb:\''.$kid.'\',typeid:\''.$typeid.'\'}">注册并发布信息</a> ';
	
	if($GLOBALS['ismethod']){//POST过程
		$data=$_POST;
	}else{
		$data=array('re'=>kc_val($_SERVER,'HTTP_REFERER'));
	}
	$data=kc_data(array('re','username','userpass','expire'));
	if($king->config('blackuser','user')){
		$array_black=explode('|',$king->config('blackuser','user'));
		$array_black=array_diff($array_black,array(null));
	}else{
		$array_black=array();
	}

	$s.=$king->openForm('index.php');	
	$_array=array(
		array('ktitle',0,'标题不能为空'),
		array('ktitle',0,20,100),
		array('kcontent',0,'内容不能为空'),
	);
	

		$s.=kc_htm_hidden(array('but'=>'add','kid'=>$kid,'typeid'=>$typeid));//这个隐藏域
		if(!$res=$king->db->getRows("select * from %s_field where modelid=12 and isuser1=1 and kid1=0 order by norder,kid;"))//全部调用
		$res=array();
		foreach($res as $rs){
		$s.=$king->portal->formdecode($rs,$data,$info,0,($kid1?2:1));
		}
		
		if(empty($user)){

			$bs .= '<div class="new_fb"><p><span class="k_error">用户尚未登陆,请登陆后发布!</span></p><div class="k_big">'.$but.'</div>';		
			$bs .= '<div class="k_big">'.$register.'</div></div>';	
		}	
		$verify=new KC_Verify_class;
		$s.=$verify->Show();
		$s.=$king->closeForm('save');

	

	
		if($data['but']=='add' && $GLOBALS['ischeck']){//有密码 并 账号验证成功的时候进行验证
				require_once '../home/library/db.class.php';
			
			
				$sql_field='ktitle,kcontent,k_thread';
				$array_field=explode(",",$sql_field); 
	
				$_array=array();//设置为空数组
				//收集字段的值
				
				foreach($array_field as $val){
					if(in_array($val,array('nshow','nhead','ncommend','nup','nfocus','nhot'))){
						//增加判断offon
						$_array[$val] = $data[$val] ? 1:0;
					}else{
						if(is_array($data[$val])){
							$_array[$val]=implode(',',$data[$val]);
						}else{
							$_array[$val]=$data[$val];
						}
						//抓图和过滤链接
						if($val=='kcontent'){
							if(kc_post('isgrab')){//抓图
								$_array[$val]=kc_grab($_array[$val]);
							}

								$_array[$val]=preg_replace('/<a ([^>]*)>|<\/a>/is','',$_array[$val]);					
		
								$_array[$val]=preg_replace('/<(table|tbody|thead|tr|td|th|caption|script|iframe) ?([^>]*)>|<\/(table|tbody|thead|tr|td|th|caption|script|iframe)>/is','',$_array[$val]);						
						
								$_array[$val]=preg_replace('/(<([^>]*))( style=)(["\'])(.*?)\4(([^>]*)\/?>)/is','$1 $6',$_array[$val]);
											
								$_array[$val]=preg_replace('/(<([^>]*))( id=)(["\'])(.*?)\4(([^>]*)\/?>)/is','$1 $6',$_array[$val]);						
							
								$_array[$val]=preg_replace('/(<([^>]*))( class=)(["\'])(.*?)\4(([^>]*)\/?>)/is','$1 $6',$_array[$val]);
						
						}
					}
				}
				if(in_array('kimage',$_array) && in_array('kcontent',$_array)){//如果有选择第一个图作为缩略图 并 kimage在列表里
					if(kc_post('isoneimage')){//抓第一张图为缩略图
						if($oneimage=preg_match('/(<img([^>]*))( src=)(["\'])(.*?)\4(([^>]*)\/?>)/is',$_array['kcontent'],$oneimage_array)){
							$smartimg=$oneimage_array[5];
							if(is_file(ROOT.substr($smartimg,strlen($king->config('inst'))))){//判断是否为本地文件
								$_array['kimage']=substr($smartimg,strlen($king->config('inst')));
							}else{//若是远程文件，则抓取
								if($path=kc_grab_get($smartimg)){//抓取成功
									if($path!=$smartimg){//值不一样，说明抓取成功
										$_array['kimage']=$path;
									}
								}
							}
						}
					}
				}
				

		
		
			
				$_array['ktag']= !empty($data['ktag']) ? $king->portal->getTag($_array['ktitle'],$_array['ktag']) : $king->portal->gettag($_array['ktitle']);
					
				if(empty($data['kdescription']) && !empty($data['kcontent'])){
					$kdescription=strip_tags($data['kcontent']);
					$kdescription=preg_replace('/(\&[a-z]{1,6};)|\s/','',$kdescription);
					$_array['kdescription']=kc_substr($kdescription,0,200);
				}
	


				$array=array(
					'kid'=>$kid,
					'typeid'=>$data['k_typeid'],		
					'userid'=>$user['userid'],
					'username'=>$user['username'],		
					'title'=>$_array['ktitle'],
					'ktag'=>$_array['ktag'],
					'kpath'=>$_array['kimage'],
					'kdescription'=>$_array['kdescription'],
					'lastuserid'=>$user['userid'],
					'lastusername'=>$user['username'],
					'lastdate'=>time(),
				);
				$db=new db;
				$threadid=$db->insert('%s_thread', $array);
				
				$array=array(
					'kid'=>$kid,
					'typeid'=>$data['k_typeid'],	
					'threadid'=>$threadid,
					'userid'=>$user['userid'],
					'username'=>$user['username'],	
					'ip'=>kc_getip(),
					'date'=>time(),
					'floor'=>$db->newid('%s_reply','threadid='.$threadid),
					'content'=>$_array['kcontent'],
				);
				$db->insert('%s_reply',$array);
				
				$goto='/browse/home/bar_thread_'.$threadid.'_1.htm';

		
				echo "<script type=\"text/javascript\">parent.location='".$goto."'</script>";
			}
		
		
		




	


	$s=str_replace('780','600',$s);


	$tmp=new KC_Template_class('template/user.htm',$king->config('templatepath').'/inside/user/edit.htm');
	$tmp->assign('main',$s);
	$tmp->assign('kuinav',$bs);
	$tmp->assign('title','添加信息');

	echo $tmp->output();
	

}
	



function king_edt(){
	global $king;
	$king->load('user');
	$king->load('portal');//


	$kid=kc_get('kid',2);
	$threadid =kc_get('threadid','0');
	if(!$rid =kc_get('rid','0')) $rid='1';
	
	


	$user=$king->user->access();


	if($user['gid']=='1'){
		$where=" threadid=".$threadid;
		$where_cont=" threadid=".$threadid." and floor=".$rid;
	}else{
		$where="userid=".$user['userid']." and threadid=".$threadid;
		$where_cont="userid=".$user['userid']." and  threadid=".$threadid." and floor=".$rid;		
	}

	if(!$body=$king->db->getRows_one("select content from %s_reply where ".$where_cont )){
		kc_error('您无此编辑权限 <a href="javascript:window.history.go(-1);">返回</a>');
	}else{
		if(!$data=$king->db->getRows_one("select * from %s_thread where ".$where )){
		}
		
			$sql_field='ktitle,kcontent,k_thread,ktag,kpath';
			
			$array_field=explode(",",$sql_field); 

			$data=kc_data($array_field,$data);
			$data['ktitle']=$data['title'];
			$data['k_typeid']=$data['typeid'];
			$data['kcontent']=$body['content'];
			
			
		
			$s.=$king->openForm('index.php?action=edt');	
			if(!$res=$king->db->getRows("select * from %s_field where modelid=12 and isuser1=1 and kid1=0 order by norder,kid;"))$res=array();
			$s=$king->openForm('index.php?action=edt');

			$s.=kc_htm_hidden(array('but'=>'add','kid'=>$data['kid'],'threadid'=>$data['threadid'],'rid'=>$rid));//这个隐藏域		
			
			
			foreach($res as $rs){
					if($rid>'1'){
						if($rs['kfield']=='ktitle' || $rs['kfield']=='ktag' || $rs['kfield']=='k_typeid'){
						}else{
						$s.=$king->portal->formdecode($rs,$data,$info,0,($kid1?2:1));
						}
					
					}else{
					$s.=$king->portal->formdecode($rs,$data,$info,0,($kid1?2:1));
					}
					
			
				
				
			}
			$verify=new KC_Verify_class;
			$s.=$verify->Show();
			$s.=$king->closeForm('save');
	}
	

				if($GLOBALS['ismethod']){//POST过程
					$data=$_POST;
				}else{
					$data=array('re'=>kc_val($_SERVER,'HTTP_REFERER'));
				}
				$data=kc_data(array('re','username','userpass','expire'));
				if($king->config('blackuser','user')){
					$array_black=explode('|',$king->config('blackuser','user'));
					$array_black=array_diff($array_black,array(null));
				}else{
					$array_black=array();
				}
	
			if($data['but']=='add' && $GLOBALS['ischeck']){//有密码 并 账号验证成功的时候进行验证
				require_once '../home/library/db.class.php';

				
				$sql_field='ktitle,kcontent,k_thread';
				$array_field=explode(",",$sql_field); 
	
				$_array=array();//设置为空数组
				//收集字段的值
				
				foreach($array_field as $val){
					if(in_array($val,array('nshow','nhead','ncommend','nup','nfocus','nhot'))){
						//增加判断offon
						$_array[$val] = $data[$val] ? 1:0;
					}else{
						if(is_array($data[$val])){
							$_array[$val]=implode(',',$data[$val]);
						}else{
							$_array[$val]=$data[$val];
						}
						//抓图和过滤链接
						if($val=='kcontent'){
							if(kc_post('isgrab')){//抓图
								$_array[$val]=kc_grab($_array[$val]);
							}
					
								$_array[$val]=preg_replace('/<a ([^>]*)>|<\/a>/is','',$_array[$val]);
					
			
								$_array[$val]=preg_replace('/<(table|tbody|thead|tr|td|th|caption|script|iframe) ?([^>]*)>|<\/(table|tbody|thead|tr|td|th|caption|script|iframe)>/is','',$_array[$val]);
							
						
								$_array[$val]=preg_replace('/(<([^>]*))( style=)(["\'])(.*?)\4(([^>]*)\/?>)/is','$1 $6',$_array[$val]);
					
								$_array[$val]=preg_replace('/(<([^>]*))( id=)(["\'])(.*?)\4(([^>]*)\/?>)/is','$1 $6',$_array[$val]);
		
								$_array[$val]=preg_replace('/(<([^>]*))( class=)(["\'])(.*?)\4(([^>]*)\/?>)/is','$1 $6',$_array[$val]);
							
						}
					}
				}
				if(in_array('kimage',$_array) && in_array('kcontent',$_array)){//如果有选择第一个图作为缩略图 并 kimage在列表里
					if(kc_post('isoneimage')){//抓第一张图为缩略图
						if($oneimage=preg_match('/(<img([^>]*))( src=)(["\'])(.*?)\4(([^>]*)\/?>)/is',$_array['kcontent'],$oneimage_array)){
							$smartimg=$oneimage_array[5];
							if(is_file(ROOT.substr($smartimg,strlen($king->config('inst'))))){//判断是否为本地文件
								$_array['kimage']=substr($smartimg,strlen($king->config('inst')));
							}else{//若是远程文件，则抓取
								if($path=kc_grab_get($smartimg)){//抓取成功
									if($path!=$smartimg){//值不一样，说明抓取成功
										$_array['kimage']=$path;
									}
								}
							}
						}
					}
				}
				

		
				$_array['ktag']= $data['ktag'];
				$_array['ktag']= !empty($data['ktag']) ? $king->portal->getTag($_array['ktitle'],$_array['ktag']) : $king->portal->gettag($_array['ktitle']);
					
				if(empty($data['kdescription']) && !empty($data['kcontent'])){
					$kdescription=strip_tags($data['kcontent']);
					$kdescription=preg_replace('/(\&[a-z]{1,6};)|\s/','',$kdescription);
					$_array['kdescription']=kc_substr($kdescription,0,200);
				}
	
	

				$array=array(
					'title'=>$_array['ktitle'],
					'ktag'=>$_array['ktag'],
					'kpath'=>$_array['kimage'],
					'kdescription'=>$_array['kdescription'],
					'lastdate'=>time(),
				);
	
					//添加&更新数据
				if($threadid && !empty($_array['ktitle'])){//update
					$king->db->update('%s_thread',$array,'threadid='.$threadid);
				}

				
				$array=array(
					'ip'=>kc_getip(),
					'date'=>time(),
					'content'=>$_array['kcontent'],
				);

								
				if(!empty($threadid) && empty($_array['ktitle']) && !empty($rid)){//update
					$king->db->update('%s_reply',$array,'threadid='.$threadid.' and floor='.$rid);
				}

				
				
				
				$goto='/browse/home/bar_thread_'.$threadid.'_1.htm#'.$rid;

		
				echo "<script type=\"text/javascript\">parent.location='".$goto."'</script>";
			}
			
	
	

	

	$s=str_replace('780','600',$s);
	
	$tmp=new KC_Template_class('template/user.htm',$king->config('templatepath').'/inside/user/edit.htm');
	$tmp->assign('main',$s);
	$tmp->assign('kuinav',$bs);
	$tmp->assign('title','编辑信息');

	echo $tmp->output();
	
	
	
	
	
	
}
		





######################################################################################

function king_ajax_edt(){
	global $king;
	require_once '../home/library/db.class.php';
	
	$king->load('user');//加载用户判断
	$king->load('portal');//
	$user=$king->user->checkLogin();//判断用户是否登录
	$kid=kc_get('kid','0');
	$typeid =kc_get('typeid','0');
	if($GLOBALS['ismethod']){//POST过程
		$data=$_POST;
	}else{
		$data=array('re'=>kc_val($_SERVER,'HTTP_REFERER'));
	}
	
	if($king->config('blackuser','user')){
		$array_black=explode('|',$king->config('blackuser','user'));
		$array_black=array_diff($array_black,array(null));
	}else{
		$array_black=array();
	}

	if(empty($user)){
			kc_ajax('OK',"<p class=\"k_err\">请先登录或注册<Br>成为会员后再发布帖子!</p>");
	}else{				
			kk_cont_tj(kc_post('ktitle'),kc_post('kcontent'),$kid,$typeid,$data,$user);
	}
}



function king_ajax_bbsedt(){

	global $king;
	require_once '../home/library/db.class.php';
	
	$king->load('user');//加载用户判断
	$king->load('portal');//
	$user=$king->user->checkLogin();//判断用户是否登录
	$threadid =kc_get('threadid','0');
	if($GLOBALS['ismethod']){//POST过程
		$data=$_POST;
	}else{
		$data=array('re'=>kc_val($_SERVER,'HTTP_REFERER'));
	}

	if($king->config('blackuser','user')){
		$array_black=explode('|',$king->config('blackuser','user'));
		$array_black=array_diff($array_black,array(null));
	}else{
		$array_black=array();
	}
	if(empty($user)){
			kc_ajax('OK',"<p class=\"k_err\">请先登录或注册<Br>成为会员后再发布帖子!</p>");
	}else{				
			$_array=array(
					array('content',0,'内容不能为空'),
			);	
			$s.=kc_htm_hidden(array('but'=>'add','threadid'=>$threadid));//这个隐藏域		
			$s.=$king->htmForm('内容','<textarea cols="60" rows="10" style="width:500px;height:100px;" id="content" name="content">'.$data['content'].'</textarea>',$_array,null,"<tt></tt>");
			$verify=new KC_Verify_class;
			$s.=$verify->Show();
			$s.=$GLOBALS['ischeck'];
			
			$but=kc_htm_a('提交','{URL:\'/browse/kuidb/index.php\',CMD:\'bbsedt\'}');	
			if($data['but']=='add' && $GLOBALS['ischeck']){//有密码 并 账号验证成功的时候进行验证

					
					require_once '../home/library/db.class.php';			
						$array=array(
							'lastuserid'=>$user['userid'],
							'lastusername'=>$user['username'],
							'lastdate'=>time(),
							'reply'=>'[[reply+1]]',
						);
						$db=new db;
						$db->update('%s_thread', $array,'threadid='.$threadid);		
						
						$kuicontent=kc_post('content');
						$kuicontent=preg_replace("/\r\n|\n|\r/", "<br />",$kuicontent);
						$kuicontent=preg_replace('/<a ([^>]*)>|<\/a>/is','',$kuicontent);
				
						$kuicontent=preg_replace('/<(table|tbody|thead|tr|td|th|caption|script|iframe) ?([^>]*)>|<\/(table|tbody|thead|tr|td|th|caption|script|iframe)>/is','',$kuicontent);
						
						$array=array(
							'threadid'=>$threadid,
							'userid'=>$user['userid'],
							'username'=>$user['username'],
							'ip'=>kc_getip(),
							'date'=>time(),
							'floor'=>$floor=$db->newid('%s_reply','threadid='.$threadid,'floor'),
							'content'=>$kuicontent
						);
						$db->insert('%s_reply',$array);
						$goto='/browse/home/bar_thread_'.$threadid.'_1.htm';
						
						$ur= "<script type=\"text/javascript\">parent.location='".$goto."'</script>";
			
						kc_ajax('OK',"<p class=\"k_ok\">成功发布".$ur."</p>");	
					
				}
			kc_ajax('商品评论',$s,$but,'',570,350);	
	}

}




function kk_cont_tj($title,$kcontent,$kid,$typeid,$data,$user){
				global $king;

				$_array=array(
				array('ktitle',0,'标题不能为空'),
				);		
				$s.=kc_htm_hidden(array('but'=>'add','kid'=>$kid,'typeid'=>$typeid));//这个隐藏域				
				$s.=$king->htmForm('标题 (20-100)字符','<input class="k_in w400" type="text" name="ktitle" value="'.htmlspecialchars($data['ktitle']).'" maxlength="100" />',$_array,null,"<tt></tt>");
				$_array=array(
					array('kcontent',0,'内容不能为空'),
				);
				$s.=$king->htmForm('内容','<textarea cols="60" rows="10" style="width:500px;height:100px;" id="kcontent" name="kcontent">'.$data['kcontent'].'</textarea>',$_array,null,"<tt></tt>");
				$verify=new KC_Verify_class;
				$s.=$verify->Show();
				$s.=$GLOBALS['ischeck'];
				$but=kc_htm_a('提交','{URL:\'/browse/kuidb/index.php\',CMD:\'edt\'}');	
				if($data['but']=='add' && $GLOBALS['ischeck']){//有密码 并 账号验证成功的时候进行验证			
					kk_post($data,$kid,$typeid,$user);			
				}
				kc_ajax('商品评论',$s,$but,'',570,350);
}



function kk_userpd($kid,$typeid){
		global $king;
		$but='<a href="javascript:;" class="k_ajax" rel="{URL:\''.$king->config('inst').'user/index.php\',CMD:\'login\',METHOD:\'GET\',kuidb:\''.$kid.'\',typeid:\''.$typeid.'\'}">'.$king->lang->get('system/install/dbuser').'</a>';
		kc_ajax($king->lang->get('portal/user/nologin'),"<p class=\"k_err\">".$king->lang->get('portal/user/nologin')."</p>",$but);
}


function kk_post($data,$kid,$typeid,$user){
	global $king;
	require_once '../home/library/db.class.php';
	$king->load('portal');//
	
				$sql_field='ktitle,kcontent,k_thread';
				$array_field=explode(",",$sql_field); 
	
				$_array=array();//设置为空数组
				//收集字段的值
				
				foreach($array_field as $val){
					if(in_array($val,array('nshow','nhead','ncommend','nup','nfocus','nhot'))){
						//增加判断offon
						$_array[$val] = $data[$val] ? 1:0;
					}else{
						if(is_array($data[$val])){
							$_array[$val]=implode(',',$data[$val]);
						}else{
							$_array[$val]=$data[$val];
						}
						//抓图和过滤链接
						if($val=='kcontent'){
	
								$_array[$val]=preg_replace('/<a([^>]*)>|<\/a>/is','',$_array[$val]);			
					
								$_array[$val]=preg_replace('/<(table|tbody|thead|tr|td|th|caption|script|iframe) ?([^>]*)>|<\/(table|tbody|thead|tr|td|th|caption|script|iframe)>/is','',$_array[$val]);
												
								$_array[$val]=preg_replace('/(<([^>]*))( style=)(["\'])(.*?)\4(([^>]*)\/?>)/is','$1 $6',$_array[$val]);						
						
								$_array[$val]=preg_replace('/(<([^>]*))( id=)(["\'])(.*?)\4(([^>]*)\/?>)/is','$1 $6',$_array[$val]);
												
								$_array[$val]=preg_replace('/(<([^>]*))( class=)(["\'])(.*?)\4(([^>]*)\/?>)/is','$1 $6',$_array[$val]);
						
						}
					}
				}
				
				
				if(empty($_array['kpath'])){
					$_array['kpath']=$king->portal->depathMode($info);
				}
		
		
				$_array['ktag']= $data['ktag'];
				$_array['ktag']= !empty($data['ktag']) ? $king->portal->getTag($_array['ktitle'],$_array['ktag']) : $king->portal->gettag($_array['ktitle']);
					
				if(empty($data['kdescription']) && !empty($data['kcontent'])){
					$kdescription=strip_tags($data['kcontent']);
					$kdescription=preg_replace('/(\&[a-z]{1,6};)|\s/','',$kdescription);
					$_array['kdescription']=kc_substr($kdescription,0,200);
				}
	


				$array=array(
					'kid'=>$kid,
					'typeid'=>$typeid,		
					'userid'=>$user['userid'],
					'username'=>$user['username'],		
					'title'=>$_array['ktitle'],
					'kpath'=>$_array['kpath'],
					'kdescription'=>$_array['kdescription'],
					'lastuserid'=>$user['userid'],
					'lastusername'=>$user['username'],
					'lastdate'=>time(),
				);
				$db=new db;
				$threadid=$db->insert('%s_thread', $array);
				
				$array=array(
					'kid'=>$kid,
					'typeid'=>$typeid,	
					'threadid'=>$threadid,
					'userid'=>$user['userid'],
					'username'=>$user['username'],	
					'ip'=>kc_getip(),
					'date'=>time(),
					'floor'=>$db->newid('%s_reply','threadid='.$threadid),
					'content'=>$_array['kcontent'],
				);
				$db->insert('%s_reply',$array);
				if($typeid=='3'){
					$ur='';
				}else{
					if($typeid=='1'){
							$goto='/browse/page/3/'.$kid;
					}elseif($typeid=='2'){
							$goto='/browse/page/4/'.$kid;
					}else{
							$goto='/browse/home/bar_thread_'.$threadid.'_1.htm#'.$rid;
					}
					$ur= "<script type=\"text/javascript\">parent.location='".$goto."'</script>";

					
					
				}
				kc_ajax('OK',"<p class=\"k_ok\">成功发布".$ur."</p>");

				
}


?>		